package com.xzp.bams.entity;

/**
 * @TODO
 * @Author xu
 * 2025/3/10
 */

import com.xzp.bams.exception.LoanException;

import java.io.Serializable;

/**
 * 可贷款储蓄
 *
 * @Author chenmin
 * 2025/3/8
 */
public class LoanSavingAccount extends SavingAccount implements Loanable, Serializable {

    //贷款金额
    private long loanAmount;

    public LoanSavingAccount() {
    }

    public LoanSavingAccount(Long id, String password, String name, String personId, String email, double balance, long loanAmount) {
        super(id, password, name, personId, email, balance);
        //this 当前正在创建的对象
        this.loanAmount = loanAmount;
    }

    /**
     * 贷款
     * @param money  贷款金额
     * @return
     */
    @Override
    public Account requestLoan(double money) throws LoanException {
        if(money>0){
            this.loanAmount+=money;
            this.setBalance(this.getBalance() + money);
        }else{
            //System.out.println("贷款额度>0");
            throw new LoanException("贷款额度>0");
        }
        //this 当前正在使用的对象
        return this;
    }

    /**
     * 还款
     * @param money  还贷款金额
     * @return
     */
    @Override
    public Account payLoan(double money)throws LoanException {
        if(money>0){
            if(money<=loanAmount){
                if(this.getBalance()>=money){
                    this.loanAmount-=money;
                    this.setBalance(this.getBalance() - money);
                }else{
                    //System.out.println("您的账户余额不够支付还款操作！");
                    throw new LoanException("您的账户余额不够支付还款操作！");
                }
            }else{
                //System.out.println("还款金额<=贷款金额");
                throw new LoanException("还款金额<=贷款金额");
            }
        }else{
            //System.out.println("还款额度>0");
            throw new LoanException("还款额度>0");
        }
        return this;
    }


    /**
     * 获取
     * @return loanAmount
     */
    public long getLoanAmount() {
        return loanAmount;
    }

    /**
     * 设置
     * @param loanAmount
     */
    public void setLoanAmount(long loanAmount) {
        this.loanAmount = loanAmount;
    }

    @Override
    public String toString() {
        return "LoanSavingAccount{" +
                "loanAmount=" + loanAmount +
                "} " + super.toString();
    }
}
